/* Copyright (C) 2010 MoSync AB

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2, as published by
the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
*/

#include "MenuButton.h"
#include "Colours.h"

MenuButton::MenuButton(int x, int y, int height, int width, Widget* parent) :
					  Widget(x, y, height, width, parent)
{
	createLayout();
}

MenuButton::MenuButton(int x, int y, int height, int width, Widget* parent,
					   MAHandle menuResource, MAHandle overlayResource,
					   MAHandle highlightResource, const char* label) :
					Widget(x, y, height, width, parent),
					menuResource(menuResource),
					overlayResource(overlayResource),
					highlightResource(highlightResource)
{
	labelText = label;
	createLayout();
}

MenuButton::~MenuButton()
{}

void MenuButton::drawWidget()
{}

void MenuButton::createLayout()
{
	mainLayout = new Layout(0, 0, this->getWidth(), this->getHeight(), this, 1, 2);
	menuImage = new Image(0, 0, this->getWidth(), this->getHeight(), mainLayout, true, true, menuResource);
	buttonText = new Label(0, 0, this->getWidth(), 32, mainLayout);
	buttonText->setCaption(labelText);
	buttonText->setHorizontalAlignment(Label::HA_CENTER);
	buttonText->setVerticalAlignment(Label::VA_CENTER);
	buttonText->setAutoSizeX(false);
	buttonText->setAutoSizeY(true);
	buttonText->setMultiLine(true);

	mainLayout->setPaddingTop(4);

	//Suppress backgrounds
	mainLayout->setDrawBackground(true);
	menuImage->setDrawBackground(false);
	buttonText->setDrawBackground(false);
	this->setDrawBackground(false);

	//Pad the menu image to the centre
	mainLayout->setHorizontalAlignment(Layout::HA_CENTER);
	mainLayout->setVerticalAlignment(Layout::VA_CENTER);

	overlayImage = new Image(0, 0, this->getWidth(), this->getHeight(), menuImage, true, true, overlayResource);
	highlightImage = new Image(0, 0, this->getWidth(), this->getHeight(), menuImage, true, true, highlightResource);

	//Suppess the highlight image
	highlightImage->setEnabled(false);
}

void MenuButton::setFont(Font* fontResource)
{
	buttonText->setFont(fontResource);
}

Font* MenuButton::getFont()
{
	return buttonText->getFont();
}

void MenuButton::setSkin(WidgetSkin* skin)
{
	mainLayout->setSkin(skin);
}

void MenuButton::setSelected(bool sel)
{
	highlightImage->setEnabled(sel);
	mainLayout->setSelected(sel);
}

void MenuButton::setMenuImage(MAHandle menuimage)
{
	menuImage->setResource(menuimage);
}

void MenuButton::setLabel(const char* label)
{
	buttonText->setCaption(label);
}

void MenuButton::setOverlayImage(MAHandle overlayimage)
{
	overlayImage->setResource(overlayimage);
}
